package com.lzx.iteam.net;

import android.content.Context;
import android.os.Message;
import android.util.Log;
import com.lzx.iteam.bean.ChatGroup;
import com.lzx.iteam.bean.SecretaryBean;
import com.lzx.iteam.provider.CloudDBOperation;
import com.lzx.iteam.provider.MessageDB;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.PreferenceUtil;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


public class SystemListMsg extends CcMsgStructure {

    private static final String TAG = "SystemListMsg";

    private Message mmCallback;
    private Context mContext;
    private CloudDBOperation cloudDBOperation;
    private MessageDB mMsgDB;

    public SystemListMsg(Message callback, Context context) {
        mmCallback = callback;
        mContext = context;
        cloudDBOperation = new CloudDBOperation(mContext);
    }

    @Override
    public void onReceive(JSONObject data) {
        ArrayList<SecretaryBean> mList = new ArrayList<SecretaryBean>();
        try {
            JSONObject jsonObject = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            int counts = jsonObject.getInt("counts");
            PreferenceUtil.getInstance(mContext).save("secretary_count",counts);
            JSONArray jsonArray = jsonObject.getJSONArray("datas");
            for (int i = 0;i<jsonArray.length();i++){
                SecretaryBean secretaryBean = new SecretaryBean();
                JSONObject object = jsonArray.getJSONObject(i);
                secretaryBean.setPush_id(object.getString("_id"));
                if (object.has("push_type")){
                    secretaryBean.setPush_type(object.getString("push_type"));
                }
                secretaryBean.setCreate_time(object.getString("create_time"));
                secretaryBean.setPush_title(object.getString("push_title"));
                secretaryBean.setExtra(object.getString("extra"));
                mList.add(secretaryBean);
            }


        } catch (JSONException e) {
            Log.e("SystemListMsg",e.toString());
        }
        if (mmCallback != null)
        {
            mmCallback.arg1 = 0;
            mmCallback.obj = mList;
            mmCallback.sendToTarget();
            return;
        }

    }

    @Override
    public void onError(int error, Object ret) {
        Log.d(TAG, "onError : " + error + " ret: " + ret);
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if(mmCallback != null){
            mmCallback.arg1 = error;
            if(!StringUtil.isEmpty(errorMsg)){
                mmCallback.obj = errorMsg;
            }else{
                mmCallback.obj = Constants.NETWORK_WRROR;
            }
            mmCallback.sendToTarget();
        }

    }
}
